<?php
	//load language package
	load_theme_textdomain( 'sakura', TEMPLATEPATH . '/languages' );
	add_action( 'widgets_init', 'sakura_widgets_init' );
	function sakura_widgets_init(){
		register_sidebar( array(
			'name' => __( 'Sakura sidebar', 'sakura' ),
			'id' => 'sakura-sidebar',
			'description' => __( 'Sakura sidebar', 'sakura' ),
			'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
			'after_widget' => '</li>',
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
		) );
	}
	function sakura_postmeta_head() {
		printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
			'meta-prep meta-prep-author',
			sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
				get_permalink(),
				esc_attr( get_the_time() ),
				get_the_date()
			),
			sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
				get_author_posts_url( get_the_author_meta( 'ID' ) ),
				sprintf( esc_attr__( 'View all posts by %s', 'twentyten' ), get_the_author() ),
				get_the_author()
			)
		);
	}
	function sakura_postmeta_end() {
	// Retrieves tag list of current post, separated by commas.
		$tag_list = get_the_tag_list( '', ', ' );
		if ( $tag_list ) {
			$posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
		} elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
			$posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
		} else {
			$posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
		}
		// Prints the string, replacing the placeholders.
		printf(
			$posted_in,
			get_the_category_list( ', ' ),
			$tag_list,
			get_permalink(),
			the_title_attribute( 'echo=0' )
		);
	}
	function sakura_404(){
		echo '<div class="nocontent"><h2>'.__('No content','sakura').'</h2>';
		echo '<p>'.__('Sorry, the content you looking for can not be found.','sakura').'</p></div>';
	}
	function my_get_recent_comments($limit = 5, $before = '<li> ', $after = '</li>') {
		global $wpdb;
		$request = "SELECT ID, comment_ID, comment_author_url, comment_content, comment_author FROM $wpdb->posts,$wpdb->comments WHERE $wpdb->posts.ID = $wpdb->comments.comment_post_ID AND (post_status = 'publish' )";
		$request .= "AND comment_approved = '1' ORDER BY $wpdb->comments.comment_date DESC LIMIT $limit";
		$comments = $wpdb->get_results($request);		
		$output = '';
		foreach ($comments as $comment) {
			$comment_author = stripslashes($comment->comment_author);
			$comment_author_url = stripslashes($comment->comment_author_url);
			$comment_content = strip_tags($comment->comment_content);
			$comment_content = stripslashes($comment_content);
			$comment_excerpt =substr($comment_content,0,150);
			$comment_excerpt = my_utf8_trim($comment_excerpt);
			$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
			if (!empty($comment_author_url)){
			$comment_author_link = '<a style="font-style: italic;color:#444;border-bottom:1px dashed #888" title=" '. $comment_author_url.'" target="_blank" href="'. $comment_author_url . '">'. $comment_author . '</a>';
			}else{
				$comment_author_link = '<a style="font-style: italic;color:#444;border-bottom:1px dashed #888" title=" View the entire comment by ' . $comment_author . '" target="_blank" href="'. $permalink  . '">'. $comment_author . '</a>';
			}
			$output .= $before . $comment_author_link . ': ' . '<a target="_blank" href="' . $permalink . '" title="View the entire comment by ' . $comment_author . '">'  . $comment_excerpt . '...' . '</a>'  . $after;
			}
			echo $output;
	}

	function my_utf8_trim($str) {
		$len = strlen($str);
		for ($i=strlen($str)-1; $i>=0; $i-=1){
			$hex .= ' '.ord($str[$i]);
			$ch = ord($str[$i]);
			if (($ch & 128)==0) return(substr($str,0,$i));
			if (($ch & 192)==192) return(substr($str,0,$i));
		}
		return($str.$hex);
	}

	function my_random_posts ($limit = 5, $length = 400, $before = '<li>', $after = '</li>', $show_pass_post = false, $show_excerpt_in_title = true) {
	    global $wpdb, $tableposts;
	    $sql = "SELECT ID, post_title, comment_count,post_date, post_content FROM $tableposts WHERE post_status = 'publish' ";
		if(!$show_pass_post) $sql .= "AND post_password ='' ";
		$sql .= "ORDER BY RAND() LIMIT $limit";
	    $posts = $wpdb->get_results($sql);
		$output = '';
	    foreach ($posts as $post) {
			$post_title = stripslashes($post->post_title);
			$post_date = mysql2date('j.m.Y', $post->post_date);
			$permalink = get_permalink($post->ID);
			$post_content = strip_tags($post->post_content); 
			$post_content = stripslashes($post_content); 
			$post_strip = substr($post_content,0,$length);
			$post_strip = utf8_trim($post_strip);
			$post_strip = str_replace('"', '', $post_strip);
			$output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="';
			if($show_excerpt_in_title) {
				$output .= $post_strip . '...  ';
				} else  {
					$output .= 'Permanent Link: ' . str_replace('"', '', $post_title) . '...   ';
			}
			$post_title .= ' (' . $post->comment_count . ')';
			$output .= $post_date . '">' . $post_title . '</a>';
			if(!$show_excerpt_in_title) {
				$output .= ': ' . $post_strip . '...  ';
	      	}
			$output .= $after;
		}
		echo $output;
	}
	
	function detect_bots(){
	$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
	if (preg_match('/google/s', $agent)) return true;
	if (preg_match('/yahoo/s', $agent)) return true;
	if (preg_match('/msn/s', $agent)) return true;
	if (preg_match('/alexa/s', $agent)) return true;
	if (preg_match('/ask/s', $agent)) return true;
	
	if (preg_match('/sogou/s', $agent)) return true;
	if (preg_match('/soso/s', $agent)) return true;
	if (preg_match('/youdao/s', $agent)) return true;
	if (preg_match('/bot/s', $agent)) return true;
	if (preg_match('/spider/s', $agent)) return true;
	if (preg_match('/crawl/s', $agent)) return true;
	//indy library
	if (preg_match('/library/s', $agent)) return true;
	if (preg_match('/rss/s', $agent)) return true;
	if (preg_match('/feed/s', $agent)) return true;
}

	
?>